package com.jrx.anytxn.transaction.job.paph.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.bean.PaphFileBean;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlDisbursementLog;
import com.jrx.anytxn.transaction.entity.TlLoanFailLog;
import com.jrx.anytxn.transaction.job.paph.step.*;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;


/**
 * 银行放款转账对账明细分库文件批量
 *
 * @author hcx
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class TraAcctDetailConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private TranAcctDetailWriter itemWriterDetail;
    @Autowired
    private TraAcctDetailprocesser traAcctDetailprocesser;
    @Autowired
    private TraAcctDetailListener traAcctDetailListener;
    @Autowired
    private GenerateLoanFailProcesser generateLoanFailProcesser;


    @Bean
    public Job traAcctDetailJob() {
        return jobBuilderFactory.get("traAcctDetailJob").incrementer(new RunIdIncrementer()).listener(traAcctDetailListener)
                .start(traAcctDetailStep())
                .next(generateLoanFailStep())
                .build();
    }

    @Bean
    public Step traAcctDetailStep() {
        return stepBuilderFactory.get("traAcctDetailStep")
                .<TlDisbursementLog, PaphFileBean>chunk(anytxnBatchProperties.getTraAcctDetailConfiguration().getChunkLimit())
                .reader(traAcctDetailFileReader())
                .processor(traAcctDetailprocesser)
                .writer(itemWriterDetail)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getTraAcctDetailConfiguration().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlDisbursementLog> traAcctDetailFileReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        org.getToday();
        return new TraAcctDetailFileReader(sqlSessionFactory, org, anytxnBatchProperties.getTraAcctDetailConfiguration().getPageSize());
    }

    @Bean
    public Step generateLoanFailStep() {
        return stepBuilderFactory.get("generateLoanFailStep")
                .<TlLoanFailLog, PaphFileBean>chunk(anytxnBatchProperties.getTraAcctDetailConfiguration().getChunkLimit())
                .reader(generateLoanFailReader())
                .processor(generateLoanFailProcesser)
                .writer(itemWriterDetail)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getTraAcctDetailConfiguration().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlLoanFailLog> generateLoanFailReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new GenerateLoanFailReader(sqlSessionFactory, org, anytxnBatchProperties.getTraAcctDetailConfiguration().getPageSize());
    }


}
